// gpio The output can only display two states: high level and low level. If a relay is connected, a high level indicates the circuit is open, and a low level indicates the circuit is closed.
import { gpioClass } from './libvbar-b-dxgpio.so'
const gpioObj = new gpioClass();
const gpio = {}

/**
 * Initialization only needs to be executed once.
 * @returns true/false
 */
gpio.init = function () {
	return gpioObj.init();
}

/**
 * Release gpio resources
 * @returns true/false
 */
gpio.deinit = function () {
	return gpioObj.exit();
}

/**
 * Apply for a GPIO; each GPIO only needs to be applied for once.
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @returns true/false
 */
gpio.request = function (gpio_) {
	let res = gpioObj.request(gpio_)
	if (!res) {
		return res
	}
	gpioObj.setFunc(gpio_, 0x04);
	return true
}

/**
 * Release the specified gpio
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @returns true/false
 */
gpio.free = function (gpio_) {
	return gpioObj.free(gpio_);
}

/**
 * Specify GPIO output high/low level
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @param {number} value Only 1 and 0 are allowed. 1 represents a high level and 0 represents a low level. The default is a high level. This field is required.
 * @returns true/false
 */
gpio.setValue = function (gpio_, value) {
	return gpioObj.setValue(gpio_, value);
}

/**
 * Get the current output of a specified GPIO: high/low level
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @returns 1 and 0, where 1 represents a high level and 0 represents a low level.
 */
gpio.getValue = function (gpio_) {
	return gpioObj.getValue(gpio_);
}

/**
 * Apply for a GPIO; each GPIO only needs to be applied for once.
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @returns true/false
 */
gpio.requestGpio = function (gpio_) {
	let res = gpioObj.request(gpio_)
	return res
}

/**
 * Configure GPIO function
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @param {number} gpio Functional attributes; different devices have different functional attributes; these are required.
 * @returns true/false
 */
gpio.setFuncGpio = function (gpio_, func) {
	let res = gpioObj.setFunc(gpio_, func)
	return res
}

/**
 * Set the specified GPIO pull-up state
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @param {number} state Set the specified GPIO pull-up state
 * @returns true/false
 */
gpio.setPullState = function (gpio_, state) {
	return gpioObj.setPullState(gpio_, state);
}

/**
 * Get the pull-up status of a specified GPIO
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @returns Pull-up state (int)
 */
gpio.getPullState = function (gpio_) {
	return gpioObj.getPullState(gpio_);
}
/**
 * Configure the driver capabilities of a specified GPIO.
 * @param {number} gpio The identifier varies depending on the device; this field is required.
 * @param {number} strength Ability (required)
 * @returns true/false
 */
gpio.setDriveStrength = function (gpio_, strength) {
	return gpioObj.setDriveStrength(gpio_, strength);
}

/**
 * Obtain the driver capabilities of a specified GPIO
 * @param {number} gpioThe identifier varies depending on the device; this field is required.
 * @returns Ability (int)
 */
gpio.getDriveStrength = function (gpio_) {
	return gpioObj.getDriveStrength(gpio_);
}

export default gpio;
